<?php

namespace controllers\admin;

use controllers\AdminApi;
use models\AuthInfo;

/**
 * Class SysUsers
 * @package controllers\admin
 * @auth    账户管理
 */
class SysUsers extends AdminApi {
    /**
     * 首页接口
     * @auth 账户列表
     * @return array
     */
    public function index() {
        $model = new \models\SysUsers();
        $map   = [];
        if (!$this->isSupper()) {
            $map['id notIn'] = [1];
        }
        list($page, $limit) = self::getPageInfo();
        $authInfo = new AuthInfo();
        $group    = new \models\AuthGroup();
        $groups   = $group->field('id,name')->select(true)->toArray('id', 'name');
        $list     = $model->field('id,nickname,account,last_time,create_time')->where($map)->order('create_time desc')->limit($page, $limit)->select(true)->each(function (&$item) use ($authInfo, $groups) {
            $item['last_time']   = timeFormat($item['last_time']);
            $item['group']       = $authInfo->getGroupName($item['id']);
            $item['groups']      = $groups;
        });

        return self::returnJson($list, $model->count($map));
    }

    /**
     * 编辑
     * @auth 编辑/添加账户
     *
     * @param null $id
     *
     * @return string
     */
    public function edit($id = null) {
        $model = new \models\SysUsers();
        $data  = $model->create(self::$post);
        if (!self::$post['group_id']) {
            return error('请选择一个分组');
        }
        if ($data['password'] || !$id) {
            if ($data['password'] != self::$post['confirmPass']) {
                return error('两次密码输入不一致');
            }
            $data['password'] = $model->generatePassword($data['password']);
        }
        if ($id) {
            // 保存操作
            $model->update($data);
            $model->recordLog(2, $this->uid);
        } else {
            // 验证占位
            $map = ['account' => self::$post['account']];
            if ($model->getFind($map, 'id')) {
                return error('用户名已被使用');
            }
            // 添加操作
            $id = $model->insert($data);
            $model->recordLog(1, $this->uid);
        }
        if ($id != 1) {
            $authInfo = new AuthInfo();
            $group_id = $authInfo->getGroupInfo($id);
            if ($group_id) {
                $authInfo->where(['uid' => $id])->update(['group_id' => self::$post['group_id']]);
            } else {
                $authInfo->insert(['uid' => $id, 'group_id' => self::$post['group_id']]);
            }
        }

        return success(($id ? '更新' : '添加') . '成功');
    }

    /**
     * 删除
     * @auth 删除账户
     *
     * @param null $id
     *
     * @return string
     */
    public function delete($id = null) {
        if ($id == 1) {
            return error('无法删除初始账号');
        }
        $model = new \models\SysUsers();
        $find  = $model->getFind($id);
        if (!$find) {
            return error('无效的账号');
        }
        $status = $model->delete($id);
        if (!$status) {
            return error('删除失败');
        }
        $model->recordLog(3, $this->uid, $find);

        return success('删除成功');
    }
}
